package com.forum.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.dao.LikesDao;
import com.forum.dao.NoteDao;
import com.forum.dao.UserDao;
import com.forum.dto.AchieveDTO;
import com.forum.entity.Concern;
import com.forum.entity.Result1;
import com.forum.entity.User;
import com.forum.service.UserService;
import com.forum.vo.AchieveVO;
import com.forum.vo.UserVO;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 用户表(User)表服务实现类
 *
 * @author makejava
 * @since 2024-04-06 14:56:33
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private LikesDao likesDao;
    @Resource
    private NoteDao noteDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer id) {
        return userDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public void insert(User user) {
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userDao.insert(user);
    }


    /**
     * 通过主键删除数据
     *
     * @param ids 主键
     * @return 是否成功
     */
    @Override
    public void deleteById(List<Integer> ids) {
        userDao.deleteById(ids);
    }

    /**
     * 修改数据
     *
     * @param user
     */
    @Override
    public void updateUser(User user) {
        user.setUpdateTime(new Date());
        userDao.update(user);
    }

    @Override
    public List<UserVO> findUser(User user,int orderByBgzCount) {
        Page<UserVO> userVOList = userDao.findUser(user);
//        if (CollectionUtils.isEmpty(userVOList)) {
//            return Collections.emptyList();  // return []
//        }

        userVOList.forEach(userInfo -> {
            /**
             * 查询关注数并插入
             */
            Integer gzId = userInfo.getId();
            Concern concern = new Concern();
            concern.setGzId(gzId);
            List<Concern> concernList = likesDao.queryByConcern(concern);
            if (!CollectionUtils.isEmpty(concernList)) {
                userInfo.setGzCount(concernList.size());
            }

            /**
             * 查询粉丝数并插入
             */
            Integer fans = userInfo.getId();
            Concern concern1 = new Concern();
            concern1.setBgzId(fans);
            List<Concern> concernList1 = likesDao.queryByConcern(concern1);
            if (!CollectionUtils.isEmpty(concernList1)) {
                userInfo.setBgzCount(concernList1.size());
            }

            //获取登录用户点赞了谁的数量
            int[] noteIds = noteDao.getNoteIdsByUserId(userInfo.getId());
            int likes = 0;
            for(int i =0;i<noteIds.length;i++) {
                likes = likes + likesDao.getUserLikes(noteIds[i]);
            }
            userInfo.setLikes(likes);
        });

        if(userVOList.size() == 1){
            return userVOList;
        }

        //简单排序  按照被关注数从高到低排序
        if(orderByBgzCount == 1){
            for (int i = 0; i < userVOList.size() - 1; i++) {
                for (int j = i + 1; j < userVOList.size(); j++) {
                    if (userVOList.get(i).getBgzCount() < userVOList.get(j).getBgzCount()) {
                        Collections.swap(userVOList, i, j);
                    }
                }
            }
        }
        return userVOList.subList(0,10);
    }

    @Override
    public UserVO findUserByNoteId(Integer noteId) {
        User user = userDao.findUserByNoteId(noteId);
        UserVO userVO = new UserVO();
        userVO.setId(user.getId());
        userVO.setAccount(user.getAccount());
        userVO.setPassword(user.getPassword());
        userVO.setUserName(user.getUserName());
        userVO.setAvatar(user.getAvatar());
        userVO.setGender(user.getGender());
        userVO.setIntroduction(user.getIntroduction());
        userVO.setCreateTime(user.getCreateTime());
        userVO.setUpdateTime(user.getUpdateTime());

        /**
         * 查询关注数并插入
         */
        Integer gzId = userVO.getId();
        Concern concern = new Concern();
        concern.setGzId(gzId);
        List<Concern> concernList = likesDao.queryByConcern(concern);
        if (!CollectionUtils.isEmpty(concernList)) {
            userVO.setGzCount(concernList.size());
        }

        /**
         * 查询粉丝数并插入
         */
        Integer fans = userVO.getId();
        Concern concern1 = new Concern();
        concern1.setBgzId(fans);
        List<Concern> concernList1 = likesDao.queryByConcern(concern1);
        if (!CollectionUtils.isEmpty(concernList1)) {
            userVO.setBgzCount(concernList1.size());
        }

        //获取总点赞数
        int likes = likesDao.getUserLikes(userVO.getId());
        userVO.setLikes(likes);

        return userVO;
    }

    //添加成就
    @Override
    public Result1 addAchieve(AchieveDTO achieve) {
        achieve.setCreateTime(LocalDateTime.now());
        userDao.addAchieve(achieve);
        return Result1.success("添加成功");
    }

    //查询成就列表
    @Override
    public List<AchieveVO> findAchieve(Integer userId) {
        List<AchieveVO> achieveDTOList = userDao.findAchieve(userId);
        return achieveDTOList;
    }
}
